﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using TerminologiaSinacor.Presenter;

public partial class page_PesquisaFontesModuloTeste : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
        if (!this.IsPostBack)
        {
            BindingModulo();

            Fontes titulo = new Fontes();
            titulo.Modulo = Request.Params["lblModuloText"];
            titulo.Fonte = Request.Params["lblFonteText"];
            titulo.NomeTermo = Request.Params["lblFonteNomeText"];
            titulo.Filtro = Request.Params["LblFiltroText"];
            
            List<Fontes> titulos = new List<Fontes>();
            titulos.Add(titulo);                    
        }

    }


    private void BindingModulo()
    {
        Modulo modulo = new Modulo();        
        TerminologiaSinacor.Utility.BindingUtil.SetDataSource(modulo.search(), DrpModulo, "codigo", "NomeModuloNomePai", "", "[Modulo]");
        TerminologiaSinacor.Utility.BindingUtil.SetDataSource(null, DrpFonte, "IdFonteIdFonteTipoNomeFonte", "FonteTipo", "", "[Fonte]");
        TerminologiaSinacor.Utility.BindingUtil.SetDataSource(null, DrpFiltro, "nome", "nome", "", "[Filtro]");
    }

    private void BindingFonte()
    {
        TxtFonte.Text = "";
        if (DrpModulo.SelectedValue != "")
        {
            Fontes fontes = new Fontes();
            fontes.Modulo = DrpModulo.SelectedValue;
            TerminologiaSinacor.Utility.BindingUtil.SetDataSource(fontes.search(fontes.Modulo), DrpFonte, "IdFonteIdFonteTipoNomeFonte", "FonteTipo", "", "[Fonte]");
        }
    }

    private void BindingFiltro()
    {
        if ((DrpModulo.SelectedValue != "") && (DrpFonte.SelectedValue != ""))
        {
            string[] prmFonte = DrpFonte.SelectedValue.Split('#');

            string IdFonte = prmFonte[0].ToString();
            string IdTipoFonte = prmFonte[1].ToString();

            Filtro filtro = new Filtro();
            TerminologiaSinacor.Utility.BindingUtil.SetDataSource(filtro.search(DrpModulo.SelectedValue, IdTipoFonte, IdFonte), DrpFiltro, "nome", "nome", "", "[Filtro]");
        }
    }

    protected void DrpFonte_SelectedIndexChanged(object sender, EventArgs e)
    {
        if ((this.DrpFonte.SelectedValue != "") && (this.DrpFonte.SelectedValue != null))
        {
            string[] prm = this.DrpFonte.SelectedValue.Split('#');
            TxtFonte.Text = prm[2];
            BindingFiltro();
        }
        else
        {
            iniciaDrpFiltro();
        }
    }

    protected void DrpModulo_SelectedIndexChanged(object sender, EventArgs e)
    {
        if ((this.DrpModulo.SelectedValue != null) && (this.DrpModulo.SelectedValue != ""))
        {
            BindingFonte();
        }
        else
        {
            iniciaDrpFonte();
            iniciaDrpFiltro();
        }
    }

    private void iniciaDrpFonte()
    {
        TerminologiaSinacor.Utility.BindingUtil.SetDataSource(null, DrpFonte, "IdFonteIdFonteTipoNomeFonte", "FonteTipo", "", "[Fonte]");
    }

    private void iniciaDrpFiltro()
    {
        TerminologiaSinacor.Utility.BindingUtil.SetDataSource(null, DrpFiltro, "nome", "nome", "", "[Filtro]");
    }

    protected void btnPesquisar_Click(object sender, EventArgs e)
    {
        if ((DrpModulo.SelectedValue != null) && (this.DrpFonte.SelectedValue != null) && (DrpFiltro.SelectedValue != null))
        {
            Fontes fontes = this.SetFiltro();            
            BindingGrid(fontes);
        }
    }

    private Fontes SetFiltro()
    {
        string TipoFonteId = "";
        if (this.DrpFonte.SelectedValue != "")
        {
            string[] prm = this.DrpFonte.SelectedValue.Split('#');
            if (prm.Length > 0) TipoFonteId = prm[1];
        }

        Fontes fontes = new Fontes();
        fontes.ModuloId = DrpModulo.SelectedValue;
        fontes.TipoFonteId =  TipoFonteId;
        fontes.Filtro = DrpFiltro.SelectedValue;
        this.ViewState["Fontes"] = fontes;
        
        return fontes;
    }

    private void BindingGrid(Fontes fontes)
    {
        List<Fontes> fontess = new List<Fontes>();
        fontess = fontes.search(fontes);
        GrdFontes.DataSource = fontess;
        GrdFontes.DataBind();
    }

    protected void GrdFontes_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        Fontes fontes = (Fontes)ViewState["Fontes"];
        this.GrdFontes.PageIndex = e.NewPageIndex;
        BindingGrid(fontes); 
    }

    //Evento responsável por alterar a cor da linha do gridview quando passo o mouse por cima
    protected void GrdFontes_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("onMouseOver", "this.style.backgroundColor='#DAA520'");
            e.Row.Attributes.Add("onMouseOut", "this.style.backgroundColor='#ffffff'");            
        }
    }
}
